package com.ai.wx.service.impl;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ai.core.constant.GlobalConstant;
import com.ai.core.dao.BaseDaoI;
import com.ai.core.pagemode.Json;
import com.ai.core.pagemode.PageFilter;
import com.ai.core.util.YKUtils;
import com.ai.wx.entity.AgentBrokerage;
import com.ai.wx.entity.AgentBrokerageDraw;
import com.ai.wx.entity.AgentUser;
import com.ai.wx.entity.PAgentBrokerageDraw;
import com.ai.wx.exception.FlowMarketException;
import com.ai.wx.service.AdminService;
import com.ai.wx.service.SystemService;

@Service
public class AdminServiceImpl implements AdminService {

	@Autowired
	private BaseDaoI<AgentUser> agentUserDao;
	@Autowired
	private BaseDaoI<AgentBrokerageDraw> agentBrokerageDrawDao;
	@Autowired
	private BaseDaoI<AgentBrokerage> agentBrokerageDao;
	@Autowired
	private BaseDaoI<PAgentBrokerageDraw> pAgentBrokerageDrawDao;
	@Autowired
	private SystemService systemService;

	public List<AgentUser> getAgentUserList(AgentUser agentUser, com.ai.core.pagemode.PageFilter ph) {
		String hql = "from AgentUser";
		Map<String, Object> params = new HashMap<String, Object>();
		hql += whereHqlAgentUser(agentUser, params);
		hql += " order by id desc ";
		List<AgentUser> list = agentUserDao.find(hql, params, ph.getPage(), ph.getRows());
		return list;
	}

	@Override
	public Long countAgentUserList(AgentUser agentUser) {
		String hql = "select count(*) from AgentUser";
		Map<String, Object> params = new HashMap<String, Object>();
		Long total = agentUserDao.count(hql + whereHqlAgentUser(agentUser, params), params);
		return total;
	}

	private String whereHqlAgentUser(AgentUser agentUser, Map<String, Object> params) {
		String hql = "";
		if (agentUser != null) {
			hql += " where state=1 ";
			if (agentUser.getId() != null) {
				hql += " and id =:agentId";
				params.put("agentId", agentUser.getId());
			}
			if (agentUser.getAgentState() != null) {
				hql += " and agentState =:agentState";
				params.put("agentState", agentUser.getAgentState());
			}
			if (StringUtils.isNotBlank(agentUser.getUserName())) {
				hql += " and userName =:userName";
				params.put("userName", agentUser.getUserName());
			}

			if (StringUtils.isNotBlank(agentUser.getPhone())) {
				hql += " and phone =:phone";
				params.put("phone", agentUser.getPhone());
			}

			if (StringUtils.isNotBlank(agentUser.getIdCard())) {
				hql += " and idCard =:idCard";
				params.put("idCard", agentUser.getIdCard());
			}

		}
		return hql;
	}

	public Json saveAgentUser(AgentUser agentUser) {
		Json j = new Json();
		AgentUser user = agentUserDao.get(AgentUser.class, agentUser.getId());
		if (user == null) {
			j.setMsg("该用户不存在");
			return j;
		} else {
			user.setAgentState(agentUser.getAgentState());
			user.setReason(agentUser.getReason());
			agentUserDao.save(user);
			j.setSuccess(true);
		}
		return j;
	}

	public Json approveDrawBrokerage(AgentBrokerageDraw draw) {
		Json j = new Json();
		try {
			AgentBrokerageDraw drawFDB = agentBrokerageDrawDao.get(AgentBrokerageDraw.class, draw.getId());
			drawFDB.setDrawState(GlobalConstant.DRAW_STATE_OK);
			drawFDB.setReply(draw.getReply());

			AgentBrokerage agentBrokerage = getAgentBrokerage(drawFDB.getAgentId());
			if (agentBrokerage != null) {
				if (agentBrokerage.getLockCharge().floatValue() != drawFDB.getDrawCharge().floatValue()) {
					j.setMsg("申请提取佣金数额出现异常[异常错误编号：]，请联系开发人员");
				} else {
					agentBrokerage.setLockCharge(0);
					agentBrokerage.setCharge(agentBrokerage.getCharge() - agentBrokerage.getLockCharge());
					agentBrokerageDao.save(agentBrokerage);
					j.setSuccess(true);
				}
			} else {
				j.setMsg("申请提取佣金数额出现异常[异常错误编号：]，请联系开发人员");
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	public Json refuseDrawBrokerage(AgentBrokerageDraw draw) throws FlowMarketException {
		Json j = new Json();
		try {
			AgentBrokerageDraw drawFDB = agentBrokerageDrawDao.get(AgentBrokerageDraw.class, draw.getId());
			drawFDB.setDrawState(GlobalConstant.DRAW_STATE_REFUSE);
			drawFDB.setReply(draw.getReply());

			AgentBrokerage agentBrokerage = getAgentBrokerage(drawFDB.getAgentId());
			if (agentBrokerage != null) {
				agentBrokerage.setLockCharge(0);
				agentBrokerageDao.save(agentBrokerage);
				j.setSuccess(true);
			} else {
				j.setMsg("申请提取佣金数额出现异常[异常错误编号：]，请联系开发人员");
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	public AgentBrokerage getAgentBrokerage(Integer agentId) {
		String hql = " from AgentBrokerage where agentId = :agentId and state = 1";
		HashMap params = new HashMap();
		params.put("agentId", agentId);
		AgentBrokerage agentBrokerage = agentBrokerageDao.get(hql, params);
		return agentBrokerage;
	}

	@Override
	public Json doLogin(String username, String password) {
		Json j = new Json();
		String userName = systemService.getUserName();
		String userPass = systemService.getPassWord();
		if (StringUtils.equals(YKUtils.encryptSalt(username), userName) && StringUtils.equals(YKUtils.encryptSalt(password), userPass)) {
			j.setSuccess(true);
		} else {
			j.setMsg("密码错误");

		}
		return j;
	}

	@Override
	public List<PAgentBrokerageDraw> getBrokerageDrawList(PAgentBrokerageDraw pAgentBrokerageDraw, PageFilter pf) {
		Map<String, Object> params = new HashMap<String, Object>();
		String sql = "select abd.id as id,abd.agent_id as agentId,abd.draw_charge as drawCharge,abd.draw_state as drawState, abd.draw_Date as drawDate,abd.notes as notes,abd.reply as reply, abd.state as state,abd.crt_Date as crtDate,au.user_name as userName,au.bank_card as bankCard,au.bank_name as bankName,"
		        + "au.email as email,au.phone as phone,au.id_card as idCard,au.id_card_pic_f as idCardPicF,au.id_card_pic_b as idCardPicB  from agent_user au, agent_brokerage_draw abd  where au.state = 1 and abd.state = 1 and au.id = abd.agent_id  ";
		sql += whereHqlPAgentBrokerageDraw(pAgentBrokerageDraw, params);
		List<PAgentBrokerageDraw> list = pAgentBrokerageDrawDao.findEntityBySql(sql, params, PAgentBrokerageDraw.class, pf.getPage(), pf.getRows());
		return list;
	}
	
	@Override
	public Long countBrokerageDrawList(PAgentBrokerageDraw pAgentBrokerageDraw) {
		Map<String, Object> params = new HashMap<String, Object>();
		String sql = "select count(*)  from agent_user au, agent_brokerage_draw abd  where au.state = 1 and abd.state = 1 and au.id = abd.agent_id  ";
		sql += whereHqlPAgentBrokerageDraw(pAgentBrokerageDraw, params);
		BigInteger total = pAgentBrokerageDrawDao.countBySql(sql, params);
		return total.longValue();
	}
	
 
	private String whereHqlPAgentBrokerageDraw(PAgentBrokerageDraw draw, Map<String, Object> params) {
		String sql = "";
		if (draw != null) {
			if (draw.getId() != null) {
				sql += " and abd.id =:id";
				params.put("id", draw.getId());
			}
			if (draw.getAgentId() != null) {
				sql += " and abd.agent_id =:agentId";
				params.put("agentId", draw.getAgentId());
			}
			if (StringUtils.isNotBlank(draw.getUserName())) {
				sql += " and au.user_name =:userName";
				params.put("userName", draw.getUserName());
			}

			if (StringUtils.isNotBlank(draw.getPhone())) {
				sql += " and au.phone =:phone";
				params.put("phone", draw.getPhone());
			}

			if (StringUtils.isNotBlank(draw.getIdCard())) {
				sql += " and au.id_card =:idCard";
				params.put("idCard", draw.getIdCard());
			}

		}
		return sql;
	} 

}
